package edu.yuhf.web.filter;

import lombok.extern.log4j.Log4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
@Log4j
public class NormalFilter implements Filter {

    public void init(FilterConfig filterConfig){}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //标准的中文字符串防乱码机制
        HttpServletRequest req = (HttpServletRequest)servletRequest;    //强制类型转换
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json ;charset=utf-8");
        resp.setCharacterEncoding("utf-8");

        resp.setHeader("Access-Control-Allow-Origin","*");  //跨域问题

        filterChain.doFilter(req,resp);
    }

    public void destroy(){}
}
